const db = require('../config/db.js');
const Sequelize = db.sequelize;
const uuid = require('uuid');
const User = Sequelize.import('../schema/user.js');
User.sync({force:false});

class UserModel{
    /**
     * 创建用户
     * @param user
     * @returns {promise<boolean>}
     */
    static async create (user) {
        let {username,password} = user;
        let id = uuid.v1();
        await User.create({
            id,
            username,
            password
        })
        return true;
    }
    /**
     * delete user
     * @param user
     * @returns {promise<boolean>}
     */
    static async delete (id) {
        // let {id,username} = user;
        await User.destroy({
            where:{
                id
            }
        });
        return true;
    }

    /**
     * search user
     * @param username
     * @returns {promise.<*>}
     */
    static async findUserByName (username) {
        return await User.findOne(
            {
              where:{
                username
              }
            }
          )
    }

    /**
     * search all 
     * @param 
     * @returns {promise.<*>}
     */
    static async findAllList () {
        return await User.findAll({
            attributes:['id','username']
        })
    }
}

module.exports = UserModel;